package com.helen.ui;

import android.graphics.Bitmap;
import android.os.Build;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.helen.shopping.R;
import com.helen.utils.MyLog;

import butterknife.BindView;

/**
 * Created by yang on 2018/5/22.
 */

public class WebViewActivity extends BaseActivity{
    @BindView(R.id.webview)
    WebView webview;
    private String url;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(getIntent().getStringExtra("title"));
        webview.setWebViewClient(new MyWebViewClient());
        url = getIntent().getStringExtra("url");
        MyLog.d("yang","url=="+url);
        WebSettings ws = webview.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setUseWideViewPort(false);  //将图片调整到适合webview的大小
//        ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webview.loadUrl(url);
    }

    @Override
    protected void updateViews() {

    }


    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
//            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webview.destroy();
    }
}
